﻿CREATE FUNCTION [util].[ConvertIntToBitString]
(@IntValue INT)
RETURNS VARCHAR (32)
AS
BEGIN
	DECLARE @ReturnValue varchar(32)
	SET @ReturnValue = ''

	DECLARE @DigitPos int
	SET @DigitPos = 0
	WHILE(@DigitPos < 31)
		BEGIN
			SET @ReturnValue =
				(CASE
					WHEN ((@IntValue & POWER(2,@DigitPos)) <> 0) THEN '1'
					ELSE '0'
				END)
				+ @ReturnValue
			SET @DigitPos = @DigitPos + 1
		END

	SET @ReturnValue =
		(CASE
			WHEN (@IntValue < 0) THEN '1'
			ELSE '0'
		END)
		+ @ReturnValue

	RETURN(@ReturnValue)
END